package com.weu.contorller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weu.bean.*;
import com.weu.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class CarController {
    @Resource
    private IBrandService iBrandService;

    @Resource
    private ICarTypeService iCarTypeService;

    @Resource
    private ISeriesService iSeriesService;

    @Resource
    private ICarService iCarService;

    @Resource
    private IModelService iModelService;

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @GetMapping("/cars")
    public String getAll(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                         @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                         Model model) {
        //为了程序的严谨性，判断非空：
        if (pageNum == null) {
            pageNum = 1;   //设置默认当前页
        }
        if (pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;    //设置默认每页显示的数据数
        }
        System.out.println("当前页是：" + pageNum + "显示条数是：" + pageSize);

        PageHelper.startPage(pageNum, pageSize);
        try {
            List<Car> list = iCarService.getAll();
            System.out.println("分页数据：" + list);
            PageInfo<Car> pageInfo = new PageInfo<Car>(list, pageSize);
            model.addAttribute("pageInfo", pageInfo);
        } finally {
            PageHelper.clearPage();
        }
        List<CarModel> carModelList = iModelService.getAll();
        model.addAttribute("carModelList", carModelList);

        //5.设置返回的jsp/html等前端页面
        return "/backstage/car/list";
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/car/{id}")
    public String deleteCar(@PathVariable("id") int id) {
        iCarService.delById(id);
        return "redirect:/cars";
    }

    /**
     * 来到前台汽车页面
     *
     * @param id
     * @param model
     * @return
     */
    @PostMapping("/car/{id}")
    public String toIndex(@PathVariable(value = "id") int id, Model model) {

        Car car = iCarService.getByCarModelId(id);
        model.addAttribute("car", car);
        //  获取所有型号(为了获取型号名称)
  //      List<CarModel> carModelList = iModelService.getAll();
//        model.addAttribute("carModelList", carModelList);

        //  获取所有系列
        //List<Series> seriesList = iSeriesService.getAll();
        //model.addAttribute("seriesList", seriesList);

        //  将数据回显到汽车展示页面
        return "shop-single-full-width";
    }


    /**
     * 来到修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/car/{id}")
    public String toEditPage(@PathVariable(value = "id") int id, Model model) {

        Car car = iCarService.getOne(id);
        model.addAttribute("car", car);
        //  获取所有型号(为了获取型号名称)
        List<CarModel> carModelList = iModelService.getAll();
        model.addAttribute("carModelList", carModelList);

        //  获取所有系列
        List<Series> seriesList = iSeriesService.getAll();
        model.addAttribute("seriesList", seriesList);

        //  将数据回显到编辑页面
        return "backstage/car/edit";
    }

    /**
     * 修改
     *
     * @param car
     * @return
     */
    @PutMapping("/car")
    public String editCar(Car car) {
        //  修改后的数据
        System.out.println("修改后的数据" + car);
        iCarService.updateCar(car);
        //  重定向
        return "redirect:/cars";
    }

    /**
     * 来到添加页面
     *
     * @param car
     * @return
     */
    @GetMapping("/car")
    public String toSavePage(Car car, Model model) {
        //  获取所有品牌
        List<Brand> brandList = iBrandService.getAll();
        model.addAttribute("brandList", brandList);

        //  获取所有类型
        List<CarType> carTypeList = iCarTypeService.getAll();
        model.addAttribute("carTypeList", carTypeList);

        //  获取所有系列
        List<Series> seriesList = iSeriesService.getAll();
        model.addAttribute("seriesList", seriesList);

        //  获取所有型号(为了获取型号名称)
        List<CarModel> carModelList = iModelService.getAll();
        model.addAttribute("carModelList", carModelList);
        return "backstage/car/save";
    }

    /**
     * 添加
     *
     * @param car
     * @return
     */
    @PostMapping("/car")
    public String saveCar(Car car) {
        System.out.println("保存的汽车：" + car);
//        car.setCarFuelConsumption(car.getCarFuelConsumption()*1.0/10.0);
        iCarService.saveCar(car);
        //  添加成功，重定向到查询页面
        return "redirect:/cars";
    }
}